(ns data2.core2
  (:use clojure.data.csv
        clojure.java.io)
  (:require [clojure.string :as string]))

;; 此程式用于归并出产权性质发生变化的企业的变更路径

(defn process
  [data]
  (->> data
       (partition-by (fn [[_ id _]] id))
       (map (fn [group]
              (->> group
                   (partition-by (fn [[own _ _]] own))
                   (map first))))
       (map (fn [group]
              (let [[_ id _] (first group)]
                [id (string/join ">" (map first group))])))))


(defn process-file
  [name]
  (with-open [in (reader name :encoding "gbk")
              out (writer (str "_" name) :encoding "gbk")]
    (write-csv out (process (read-csv in)))))
